package com.lewis.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.lewis.entry.User;
import com.lewis.mapper.UserMapper;
import com.lewis.vo.PemsVo;

@RestController
public class DynamicControllerTest {
	
	@Resource
	private UserMapper userMapper;
	
	
	/**
	 * 传参则查询一条，不传参数为null则查询全部记录
	 * <where></where>
	 * @param username
	 * @return
	 * @throws Exception
	 */
	@RequiresPermissions("user:addd")
	@GetMapping(path={"/getUser/{username}","/getUser"})
	public List<User> testIf(@PathVariable(value="username",required=false)String username) throws Exception{
		
		Subject subject=SecurityUtils.getSubject();
		
		System.out.println("sessionID:*******************************"+subject.getSession().getId());
		
		System.out.println("是否处于登录中"+subject.isAuthenticated());
		return userMapper.getUser(username);
		
	}
	
	
	@GetMapping("/selectUser/{sex}/{age}")
	public List<User> testWhere(@PathVariable(name="sex",required=true)String sex,@PathVariable(name="age",required=false)int age){
		
		return null;
	}
	
	/**
	 * <set><if test="***"></if></set>
	 * @param user
	 * @throws Exception 
	 */
	@PostMapping("/updateUser")
	public void testUpdateSet(@RequestBody User user) throws Exception{
		
		userMapper.updateUser(user);
	}
	
	/**
	 * username->permissions
	 * @return
	 * @throws Exception 
	 */
	@GetMapping("/getPerms/{username}")
	public PemsVo testGetPerms(@PathVariable("username")String username) throws Exception{
		return userMapper.getPermis(username);
	}
	
	
	@GetMapping("/logout")
	public boolean doLogout(){
		Subject subject=SecurityUtils.getSubject();
		subject.logout();
		return subject.isAuthenticated();
	}

}
